#include "ActorBarrel.h"

ActorBarrel::ActorBarrel(CL_GraphicContext gc, StateSet* states){
	this->states=states;
	this->grafic=new SpriteBarrel(gc);
	downdelay = 0;
	actualState = 0;
	ready=false;
	setPosition(160, 205);
}
#define BARREL_STEP 1
void ActorBarrel::process()
{
	if(actualState >= states->Count()){
		return;
	}
	switch(states->GetState(actualState).getDirection()){
	case barrelLeft:
		setPosition(getPosition().x-BARREL_STEP, getPosition().y);
		grafic->drawrollingbarrel(getPosition());
		break;
	case barrelRight:
		setPosition(getPosition().x+BARREL_STEP, getPosition().y);
		grafic->drawrollingbarrel(getPosition());
		break;
	case barrelDown:
		setPosition(getPosition().x, getPosition().y+BARREL_STEP);
		grafic->drawrollingbarrel(getPosition());
		break;
	case barrelLeftDown:
		if(downdelay == 15)
		{
			setPosition(getPosition().x-BARREL_STEP, getPosition().y+BARREL_STEP);
			downdelay=0;
		}
		else
		{
			setPosition(getPosition().x-BARREL_STEP, getPosition().y);
			downdelay++;
		}
		grafic->drawrollingbarrel(getPosition());
		break;
	case barrelRightDown:
		if(downdelay == 15)
		{
			setPosition(getPosition().x+BARREL_STEP, getPosition().y+BARREL_STEP);
			downdelay=0;
		}
		else
		{
			setPosition(getPosition().x+BARREL_STEP, getPosition().y);
			downdelay++;
		}
		grafic->drawrollingbarrel(getPosition());
		break;
	case ldown:
		setPosition(getPosition().x, getPosition().y+BARREL_STEP);
		grafic->drawfallingbarrel(getPosition());
		break;
	}

	if(CollisionDetection::giveme()->enofstate(states->GetState(actualState).getEscapePoint(), getPosition()))
	{
		actualState++;
		if(actualState >= states->Count())// 13)
		{
			ready=true;
		}

	}
}

void ActorBarrel::reset(){
	actualState = 0;
	setPosition(160, 205);
}

void ActorBarrel::drawabarrel(CL_Vec2<int> pos)
{
	grafic->drawrollingbarrel(pos);
}

ActorBarrel::~ActorBarrel(){
	delete states;
}